package com.example.servlet.flight;

import com.example.entity.City;
import com.example.entity.FlightDetail;
import com.example.entity.Manager;
import com.example.entity.User;
import com.example.service.FlightService;
import com.example.service.impl.FlightServiceImpl;
import com.example.utils.MybatisUtil;
import com.example.utils.ThymeleafUtil;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.thymeleaf.context.Context;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/manager_flight_search1")
public class mFlightSearch1Servlet extends HttpServlet {
    private FlightService service;
    private SqlSession sqlSession;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        sqlSession = MybatisUtil.getSession();
        service = new FlightServiceImpl(sqlSession);
    }

    @Override
    public void destroy() {
        sqlSession.close();
        super.destroy();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Manager manager = (Manager) req.getSession().getAttribute("manager");
        String departureCity = req.getParameter("departureCity");
        String arrivalCity = req.getParameter("arrivalCity");
        String departureDateStr = req.getParameter("departureDate");
        Date departureDate = null;
        try {
            if(departureDateStr != null){
                departureDate = new SimpleDateFormat("yyyy-MM-dd").parse(departureDateStr);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Context context = new Context();
        if(departureCity == null || arrivalCity == null || departureDateStr == null){
            context.setVariable("null", true);
        }else if(departureCity.equals(arrivalCity)){
            context.setVariable("city_same", true);
        }else{
            List<FlightDetail> flightDetails = new ArrayList<>();
            if (service.isCityExist(departureCity) == 0 && service.isCityExist(arrivalCity) == 0) {
                flightDetails.addAll(service.searchFlightDetails(departureCity, arrivalCity, departureDate));
            } else if (service.isCityExist(departureCity) == 0 && service.isCityExist(arrivalCity) !=0 ) {
                context.setVariable("no_arrival", true);
                City city = service.getNearCity(arrivalCity);
                String arrivalCity1 = city.getCity1();
                String arrivalCity2 = city.getCity2();
                flightDetails.addAll(service.searchFlightDetails(departureCity, arrivalCity1, departureDate));
                flightDetails.addAll(service.searchFlightDetails(departureCity, arrivalCity2, departureDate));
            } else if (service.isCityExist(departureCity) != 0 && service.isCityExist(arrivalCity) == 0) {
                context.setVariable("no_departure", true);
                City city = service.getNearCity(departureCity);
                String departureCity1 = city.getCity1();
                String departureCity2 = city.getCity2();
                flightDetails.addAll(service.searchFlightDetails(departureCity1, arrivalCity, departureDate));
                flightDetails.addAll(service.searchFlightDetails(departureCity2, arrivalCity, departureDate));
            } else{
                context.setVariable("no_departure_arrival", true);
                City city1 = service.getNearCity(departureCity);
                City city2 = service.getNearCity(arrivalCity);
                flightDetails.addAll(service.searchFlightDetails(city1.getCity1(), city2.getCity1(), departureDate));
                flightDetails.addAll(service.searchFlightDetails(city1.getCity1(), city2.getCity2(), departureDate));
                flightDetails.addAll(service.searchFlightDetails(city1.getCity2(), city2.getCity1(), departureDate));
                flightDetails.addAll(service.searchFlightDetails(city1.getCity2(), city2.getCity2(), departureDate));
            }
            context.setVariable("flightdetails", flightDetails);
            context.setVariable("departure_place", departureCity);
            context.setVariable("arrival_place", arrivalCity);
            context.setVariable("departureDate", departureDateStr);
        }
        context.setVariable("managername", manager.getM_name());
        ThymeleafUtil.process("manager_flight_result1.html", context, resp.getWriter());
    }
}
